<template>
  <div class="navbar">
    <div class="left-menu">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/back/dashboard' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>{{ route.meta.title }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    
    <div class="right-menu">
      <div class="right-menu-item" @click="toggleFullScreen">
        <el-icon :size="20">
          <component :is="isFullscreen ? Aim : FullScreen" />
        </el-icon>
      </div>
      
      <el-dropdown trigger="click">
        <div class="avatar-wrapper">
          <el-avatar 
            :size="32" 
            :src="avatarUrl"
            @error="handleAvatarError"
          >
            {{ userInfo?.name?.charAt(0)?.toUpperCase() || userInfo?.username?.charAt(0)?.toUpperCase() || 'U' }}
          </el-avatar>
          <span class="user-name">{{ userInfo?.name || userInfo?.username }}</span>
          <el-icon class="el-icon--right">
            <ArrowDown />
          </el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="handleProfile">
              <el-icon><User /></el-icon>
              个人信息
            </el-dropdown-item>
            <el-dropdown-item divided @click="handleLogout">
              <el-icon><SwitchButton /></el-icon>
              退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/user'
import { ArrowDown, User, SwitchButton, FullScreen, Aim } from '@element-plus/icons-vue'

const API_BASE_URL = process.env.VUE_APP_BASE_API || '/api'
const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const userInfo = computed(() => userStore.userInfo)
const isFullscreen = ref(false)
const avatarLoadError = ref(false)

// 计算头像URL
const avatarUrl = computed(() => {
  if (avatarLoadError.value || !userInfo.value?.avatar) {
    return ''
  }
  
  const url = userInfo.value.avatar
  
  // 如果已经是完整URL(包含http或https)，则直接返回
  if (url.startsWith('http://') || url.startsWith('https://')) {
    return url
  }
  
  // 如果已经包含API前缀，则直接返回
  if (url.startsWith(API_BASE_URL)) {
    return url
  }
  
  // 否则添加API前缀
  return `${API_BASE_URL}${url.startsWith('/') ? '' : '/'}${url}`
})

// 处理头像加载错误
const handleAvatarError = () => {
  avatarLoadError.value = true
}

const toggleFullScreen = () => {
  if (!document.fullscreenElement) {
    document.documentElement.requestFullscreen()
    isFullscreen.value = true
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen()
      isFullscreen.value = false
    }
  }
}

document.addEventListener('fullscreenchange', () => {
  isFullscreen.value = !!document.fullscreenElement
})

const handleProfile = () => {
  router.push('/back/personInfo/profile')
}

const handleLogout = async () => {
  await userStore.logout()
  router.push('/login')
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: #ffffff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .left-menu {
    display: flex;
    align-items: center;
    gap: 16px;

    :deep(.el-breadcrumb__inner) {
      color: #666;
      line-height: 32px;
      
      &.is-link {
        color: #999;
        
        &:hover {
          color: #409eff;
        }
      }
    }
  }

  .right-menu {
    display: flex;
    align-items: center;
    gap: 12px;

    .right-menu-item {
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      color: #666;
      border-radius: 4px;
      transition: all 0.3s;
      height: 32px;
      width: 32px;
      
      &:hover {
        background: #f6f6f6;
        color: #409eff;
      }
    }
    
    .avatar-wrapper {
      display: flex;
      align-items: center;
      padding: 4px 12px;
      height: 40px;
      cursor: pointer;
      border-radius: 20px;
      transition: all 0.3s;
      
      &:hover {
        background: #f6f6f6;
        
        .el-icon {
          color: #409eff;
        }
      }
      
      .el-avatar {
        border: 2px solid transparent;
        transition: all 0.3s;
        background: #f0f2f5;
        
        &:hover {
          border-color: #409eff;
          transform: scale(1.1);
        }
      }
      
      .user-name {
        margin: 0 8px;
        font-size: 14px;
        color: #666;
        line-height: 32px;
        transition: all 0.3s;
        
        &:hover {
          color: #409eff;
        }
      }

      .el-icon {
        color: #999;
        display: flex;
        align-items: center;
        transition: all 0.3s;
      }
    }
  }

  :deep(.el-dropdown-menu__item) {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    height: 40px;
    transition: all 0.3s;
    
    .el-icon {
      margin-right: 4px;
      display: flex;
      align-items: center;
      transition: all 0.3s;
    }
    
    &:hover {
      background-color: #ecf5ff;
      color: #409eff;
      
      .el-icon {
        color: #409eff;
      }
    }
  }
}
</style> 